package com.qf.dao.impl;

import com.qf.dao.CardDao;
import com.qf.model.Card;
import com.qf.utils.DruidUtil;
import org.apache.commons.dbutils.QueryRunner;
import java.util.List;
import java.sql.Connection;
import java.sql.Date;
import java.util.Calendar;
import java.util.Random;

public class CardDaoImpl extends BaseDao implements CardDao {
    QueryRunner queryRunner = new QueryRunner(DruidUtil.getDataSource());

    //添加申请的信用卡   事务
    @Override
    public int addCreditCard(Connection connection, int u_id, String c_account, String c_phone) {
        try {
            String sql = "insert into card (c_account,c_phone,c_type,u_id) values (?,?,1,?)";
            int update = queryRunner.update(sql, c_account, c_phone, u_id);
            return update;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    //添加申请的信用卡信息 非事务
    @Override
    public int addCreditCard(int u_id, String c_account, String c_phone) {
        String sql = "insert into card (c_account,c_phone,c_type,u_id) values (?,?,1,?)";
        int update = update(sql, c_account, c_phone, u_id);
        return update;
    }


    //根据用户id查询用户是否具有可使用的储蓄卡
    @Override
    public List<Card> findDepositCardByU_id(int u_id) {
        String sql = "select * from card where c_check = 1 and c_type = 0 and u_id = ?";
        List<Card> list = queryForList(Card.class,sql,u_id);
        return list;
    }

    //根据信用卡卡号进行信用卡还款
    @Override
    public int repaymentAmount(Connection connection, Card card, double amount) {
        try {
            String sql = "update card set c_balance = c_balance + ? where c_id = ?";
//            int update = update(sql,amount,card.getC_id());
            int update = queryRunner.update(connection, sql, amount, card.getC_id());
            return update;
        }catch (Exception e){
            e.printStackTrace();
        }
        return 0;
    }

    //根据信用卡号，查询信用卡信息
    @Override
    public Card findCreditCardByC_id(String c_id) {
        String sql = "select * from card where c_id = ?";
        return queryForOne(Card.class,sql,c_id);
    }

    //通过用户id查询是否有申请过信用卡信用卡
    @Override
    public List<Card> findCreditCardAll(int u_id) {
        String sql = "select * from card where c_type = 1 and u_id = ?";
        List<Card> list = queryForList(Card.class,sql,u_id);
        return list;
    }

    //根据用户id查询用户是否具有未审批通过信用卡或已拒绝的信用卡
    @Override
    public List<Card> findCreditCardUnusable(int u_id) {
        String sql = "select * from card where c_type = 1 and u_id = ? and c_check = 0 or c_check=2 ";
        List<Card> list = queryForList(Card.class,sql,u_id);
        return list;
    }
    //查询用户是否有主卡
    @Override
    public List<Card> findHostCard(int u_id) {
        String sql = "select * from card where u_id = ? and c_bind = 2";
        List<Card> list = queryForList(Card.class, sql, u_id);
        return list;
    }
    //审核或者拒绝未审核的银行卡，或者信用卡 非事务
    @Override
    public int updateCard(int id, int type) {
        //type  1: 已审核 2：审核被拒绝

        String sql = "";

        if (type == 1) {
            //生成随机卡号
            Calendar calendar = Calendar.getInstance();
            // 获取当前年
            int year =calendar.get(Calendar.YEAR);
            // 获取当前月
            int month = calendar.get(Calendar.MONTH) + 1;
            // 获取当前日
            int day = calendar.get(Calendar.DATE);
            // 获取当前小时
            int hour = calendar.get(Calendar.HOUR_OF_DAY);
            // 获取当前分钟
            int minute = calendar.get(Calendar.MINUTE);

// 获取当前秒
            int second = calendar.get(Calendar.SECOND);
            Random r= new Random();
            //生成卡号
            String s=""+year+month+day+hour+minute+second;
            for(int i=0;i<7;i++){
                s+=r.nextInt(9);
            }
            sql = "update card set c_check=1,c_id="+s+" where id=?";
        } else {
            sql = "update card set c_check=2 where id=?";
        }
        return update(sql,id);
    }

    //根据银行卡卡号查询银行卡信息
    @Override
    public Card findCard(String c_id) {
        String sql = "select * from card where c_id = ?";
        return queryForOne(Card.class,sql,c_id);
    }
}
